package com.A21.Adapter;

import java.util.ArrayList;  
import java.util.HashMap;
import java.util.Locale;

import com.A21.Object.MSTCustomer;
import com.A21MobileSales.R;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class CustomerListAdapter extends BaseAdapter   {      
    ViewHolder holder;
    LayoutInflater inflater;
    ArrayList<MSTCustomer>  CustomerList;
    ArrayList<MSTCustomer> mOriginalValues;
    static final String KEY_CCode = "CCode";
    static final String KEY_CName = "CName";
    private Context context;
    public CustomerListAdapter() {
		// TODO Auto-generated constructor stub
	} 
	public CustomerListAdapter(Context _context,ArrayList<MSTCustomer> map) {  
		Activity act = (Activity) _context;
		this.context=_context;
		this.CustomerList = map;  
		this.mOriginalValues =new  ArrayList<MSTCustomer>();
		this.mOriginalValues.addAll(CustomerList);
		inflater = (LayoutInflater) act
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
     
    public int getCount() {  
        return CustomerList.size();  
    }  
    public Object getItem(int arg0) {  
        return CustomerList.get(arg0);  
    }  
    public long getItemId(int arg0) {  
        return 0;  
    }  
       
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
		  View vi=convertView;
	      if(convertView==null){ 
	         vi = inflater.inflate(R.layout.row_customerselection, null);
	         holder = new ViewHolder();
	         holder.txtCCode = (TextView)vi.findViewById(R.id.CSLT_txtCustomerCode);
	         holder.txtCName = (TextView)vi.findViewById(R.id.CSLT_txtCUstomerName);
	        vi.setTag(holder);
	      }
	      else{ 
	    	holder = (ViewHolder)vi.getTag();
	       } 
	      try
	      {
	    	  holder.txtCCode.setText(CustomerList.get(position).Code); 
	    	  holder.txtCName.setText(CustomerList.get(position).CName); 
	      }
	      catch(Exception ex)
	      {
	    	  ex.printStackTrace();
	      }
	          
	     
	      return vi;
	}
    
    private class ViewHolder {
    	   TextView txtCCode;
    	   TextView txtCName;
    	  }
        
    public void filter(String charText) {
		charText = charText.toLowerCase(Locale.getDefault());
		CustomerList.clear();
		if (charText.length() == 0) {
			CustomerList.addAll(mOriginalValues);
		} else {
			for ( MSTCustomer  pc: mOriginalValues) {
				final String Code = pc.Code;
                final String Name = pc.CName;
                if (Code.toLowerCase().contains(charText.toString().toLowerCase()) || 
                		Name.toLowerCase().contains(charText.toString().toLowerCase())){ 
                	CustomerList.add(pc);
                }
			}
		}
		notifyDataSetChanged();
	}
} 